#include <stdio.h>
#include <errno.h>
#include <signal.h>
#include "watchworker.h"


int InitProcess(int iMaxNum)
{
	int num_childs = iMaxNum;
	if (num_childs > 0) {
		int child = 0;
		while (!child) {
			if (num_childs > 0) {
				switch (fork()) {
				case -1:
					exit(-1);
				case 0:
					child = 1;
					break;
				default:
					num_childs--;
					break;
				}
			} else {
				int status;
				if (-1 != wait(&status)) {
					num_childs++;
				} else {
					switch (errno) {
					case EINTR:
						kill(0, SIGHUP);
						break;
					default:
						break;
					}
				}
			}
		}

		/**
		 * for the parent this is the exit-point 
		 */
		if (!child) {
			kill(0, SIGINT);
			exit(0);
		}
	}
}
